<template>
  <template v-for="(item, index) in props.menuData" :key="`${props.index}-${item.meta.id}`">
    <!-- 无子菜单情况 -->
    <el-menu-item 
      @click="handleClick(item, `${props.index}-${item.meta.id}`)"
      v-if="!item.children || item.children.length==0" 
      :index="`${props.index}-${item.meta.id}`">
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon> 
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <!-- 有子菜单情况 -->
    <el-sub-menu v-else :index="`${props.index}-${item.meta.id}`">
      <template #title>
        <el-icon size="20">
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <tree-menu :menuData="item.children" :index="`${props.index}-${item.meta.id}`"/>
    </el-sub-menu>
  </template>
</template>

<script setup>
import { useRouter } from 'vue-router'
// 获取父组件传参
const props = defineProps(['menuData', 'index'])

// 创建路由实例
const router = useRouter();

// 点击菜单
const handleClick = (item, active) => {
  router.push(item.meta.path)
}
</script>

<style lang="less" scoped>

</style>